﻿using System.Linq;
using System.Web.Mvc;
using MRGSP.SurveyMaster.Core.Service;
using MRGSP.SurveyMaster.Core.Service.Utils;
using MRGSP.SurveyMaster.Infra.Dto;

namespace MRGSP.SurveyMaster.WebUI.Controllers
{
    [Authorize]
    public class SurveySessionController : Controller
    {
        private readonly ISurveySessionSevice surveySessionService;
        private readonly IUserService userService;
        private readonly IQuestionService questionService;
        private readonly ISurveyReader surveyReader;
        private readonly ISurveyValidator surveyValidator;
        private readonly IQuestionManager questionManager;
        private readonly ISurveyService surveyService;

        public SurveySessionController(ISurveySessionSevice surveySessionService, IUserService userService, IQuestionService questionService, ISurveyReader surveyReader, ISurveyValidator surveyValidator, IQuestionManager questionManager, ISurveyService surveyService)
        {
            this.surveySessionService = surveySessionService;
            this.surveyService = surveyService;
            this.questionManager = questionManager;
            this.surveyValidator = surveyValidator;
            this.surveyReader = surveyReader;
            this.questionService = questionService;
            this.userService = userService;
        }

        public ActionResult Index(int? page)
        {
            return View(surveySessionService.GetPage(page ?? 1, 10, UserId()));
        }

        [HttpPost]
        public ActionResult Done(long id)
        {
            surveySessionService.Done(id);
            return RedirectToAction("Index");
        }

        public ActionResult Start(long surveyId)
        {
            ViewData["survey"] = surveyService.Get(surveyId);
            var questions = questionService.GetBySurveyId(surveyId);
            return View(new StartSurveyInput { Questions = questions, SurveyId = surveyId });
        }

        [HttpPost]
        public ActionResult Start(FormCollection formCollection, long surveyId)
        {

            var form = formCollection.AllKeys.ToDictionary(k => k, v => formCollection[v]);
            var questions = questionService.GetBySurveyId(surveyId).ToList();
            var answers = surveyReader.Read(questions, form).ToList();
            var messages = surveyValidator.Validate(questions, answers).ToList();

            if (messages.Count > 0)
            {
                foreach (var m in messages)
                    ModelState.AddModelError("q" + m.QuestionId, m.Message);

                questionManager.RebuildSurveyQuestions(questions, answers);
                questionManager.ColorizeErrors(questions, messages);
                ViewData["survey"] = surveyService.Get(surveyId);
                return View(new StartSurveyInput { Questions = questions, SurveyId = surveyId });
            }

            surveySessionService.Save(surveyId, UserId(), answers);
            return RedirectToAction("Success");
        }

        public ActionResult Review(long id)
        {
            var session = surveySessionService.Get(id);
            var questions = questionService.GetBySurveyId(session.SurveyId).ToList();
            var answers = surveySessionService.GetBySessionId(id).ToList();
            questionManager.RebuildSurveyQuestions(questions, answers);
            ViewData["survey"] = surveyService.Get(session.SurveyId);
            return View(new ReviewSurveyInput { Questions = questions, SessionId = id });
        }

        [HttpPost]
        public ActionResult Review(FormCollection formCollection, long sessionId)
        {
            var form = formCollection.AllKeys.ToDictionary(k => k, v => formCollection[v]);
            var session = surveySessionService.Get(sessionId);
            var questions = questionService.GetBySurveyId(session.SurveyId).ToArray();
            var answers = surveyReader.Read(questions, form).ToList();
            var messages = surveyValidator.Validate(questions, answers).ToList();

            if (messages.Count > 0)
            {
                foreach (var m in messages)
                {
                    ModelState.AddModelError("q" + m.QuestionId, m.Message);
                }

                questionManager.RebuildSurveyQuestions(questions, answers);
                questionManager.ColorizeErrors(questions, messages);
                ViewData["survey"] = surveyService.Get(session.SurveyId);
                return View(new ReviewSurveyInput { Questions = questions, SessionId = sessionId });
            }

            surveySessionService.Update(sessionId, answers);
            return RedirectToAction("Success");
        }

        private long UserId()
        {
            return userService.Get(User.Identity.Name).Id;
        }

        public ActionResult Success()
        {
            return View();
        }
    }


}